#!/bin/bash

filelist="filelist.json"
cachedir="../../cache"
builddir="../../build"
buildpydir="$builddir/py"

if [ ! -d $cachedir ] ; then
    mkdir $cachedir
fi
if [ ! -d $builddir ] ; then
    mkdir $builddir
fi
if [ ! -d $buildpydir ] ; then
    mkdir $buildpydir
fi

jsonfile="../../$(cat $filelist | grep xrnarchive | grep json | tr -d ' ' | tr -d ',' | tr -d '"')"
templatefile="../../$(cat $filelist | grep xrnarchive | grep template | tr -d ' ' | tr -d ',' | tr -d '"')"

tmpfile="$cachedir/xrnarchivejsonstr.py"
outfile="$buildpydir/xrnarchive-cli"

# embed json file into a python script
cat $jsonfile | sed 's/$/\\/' | sed '1s/^/toolsjsonstr = + / ; $s/$/\n+/ ' | tr -s "+" "'" > $tmpfile

# create the output python file
cat $tmpfile $templatefile | sed '1s/^/#!\/usr\/bin\/env python3\n\n/' > $outfile

rm $tmpfile
chmod 777 $outfile
